#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 7;

struct SMT {
    int s[N << 2], tag[N << 2];

    inline int ls(int x) {
        return x << 1;
    }

    inline int rs(int x) {
        return x << 1 | 1;
    }

    inline void spread(int x, int k) {
        tag[x] += k, s[x] += k;
    }

    inline void pushdown(int x) {
        if (tag[x])
            spread(ls(x), tag[x]), spread(rs(x), tag[x]), tag[x] = 0;
    }

    void update(int x, int nl, int nr, int l, int r, int k) {
        if (l <= nl && nr <= r) {
            spread(x, k);
            return;
        }
        
        pushdown(x);
        int mid = (nl + nr) >> 1;
        
        if (l <= mid)
            update(ls(x), nl, mid, l, r, k);
        
        if (r > mid)
            update(rs(x), mid + 1, nr, l, r, k);
        
        s[x] = max(s[ls(x)], s[rs(x)]);
    }

    int query(int x, int nl, int nr, int l, int r) {
        if (l <= nl && nr <= r)
            return s[x];
        
        pushdown(x);
        int mid = (nl + nr) >> 1;

        if (r <= mid)
            return query(ls(x), nl, mid, l, r);
        else if (l > mid)
            return query(rs(x), mid + 1, nr, l, r);
        else
            return max(query(ls(x), nl, mid, l, r), query(rs(x), mid + 1, nr, l, r));
    }
} LIS, LDS;

pair<int, int> updlis[N], updlds[N];

int a[N], b[N], prelis[N], prelds[N];

int n;

signed main() {
    freopen("shung.in", "r", stdin);
    freopen("shung.out", "w", stdout);
    scanf("%d", &n);

    for (int i = 1; i <= n; ++i)
        scanf("%d", a + i);

    fill(b + 1, b + 1 + n, 0);

    for (int i = n; i; --i) {
        int cur = lower_bound(b + 1, b + 1 + n, -a[i]) - b;
        updlis[i] = make_pair(-b[cur] + 1, a[i]), b[cur] = -a[i];
        LIS.update(1, 1, n, updlis[i].first, updlis[i].second, 1);
    }

    fill(b + 1, b + 1 + n, n + 1);

    for (int i = n; i; --i) {
        int cur = lower_bound(b + 1, b + 1 + n, a[i]) - b;
        updlds[i] = make_pair(a[i], b[cur] - 1), b[cur] = a[i];
        LDS.update(1, 1, n, updlds[i].first, updlds[i].second, 1);
    }

    set<int> st = {0, n + 1};
    int ans = 0;

    for (int i = 1; i <= n; ++i) {
        LIS.update(1, 1, n, updlis[i].first, updlis[i].second, -1);
        LDS.update(1, 1, n, updlds[i].first, updlds[i].second, -1);
        int w = a[i], pre = *prev(st.lower_bound(w)), nxt = *st.lower_bound(w);
        ans = max(ans, (pre >= 1 ? LDS.query(1, 1, n, 1, pre) : 0) + LIS.query(1, 1, n, w, w) + 1);
        ans = max(ans, (nxt <= n ? LIS.query(1, 1, n, nxt, n) : 0) + LDS.query(1, 1, n, w, w) + 1);

        if (pre >= 1)
            LIS.update(1, 1, n, pre, pre, 1);

        if (nxt <= n)
            LDS.update(1, 1, n, nxt, nxt, 1);

        prelis[w] = (pre >= 1 ? prelis[pre] : 0) + 1, prelds[w] = (nxt <= n ? prelds[nxt] : 0) + 1;
        LIS.update(1, 1, n, w, w, prelis[w] + prelds[w] - 1);
        LDS.update(1, 1, n, w, w, prelis[w] + prelds[w] - 1);
        st.emplace(w);
    }

    printf("%d\n", ans);
    return 0;
}
